package com.cly.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cly.anno.Log;
import com.cly.domain.SysUser;
import com.cly.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "后台用户接口")
@RequestMapping("sys/user")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    /**
     * 获取当前用户登录的信息
     * @return
     */
    @GetMapping("info")
    @ApiOperation(value = "获取当前用户登录信息")
    public ResponseEntity<SysUser> getUserInfo(){
        //获取当前用户
        String userId = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        SysUser sysUser = sysUserService.getById(Long.valueOf(userId));
        return ResponseEntity.ok(sysUser);
    }

    /**
     * 分页查询管理员的接口
     * @return
     */
    @GetMapping("page")
    @PreAuthorize("hasAuthority('sys:user:page')")
    @ApiOperation("分页查询管理员接口")
    public ResponseEntity<Page<SysUser>> loadSysUserPage(Page<SysUser> page, SysUser sysUser){
        Page<SysUser> sysUserPage = sysUserService.findSysUserByPage(page,sysUser);
        return ResponseEntity.ok(sysUserPage);
    }

    /**
     * 新增后台管理员
     * @return
     */
    @PostMapping
    @ApiOperation("新增后台管理员")
    @PreAuthorize("hasAuthority('sys:user:save')")
    public ResponseEntity<Void> addSysUser(@RequestBody SysUser sysUser){
        //新增管理员需要知道是谁做的
        String createUserId = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        sysUser.setCreateUserId(Long.valueOf(createUserId));
        //调用service做新增
        sysUserService.save(sysUser);
        return ResponseEntity.ok().build();

    }

    /**
     * 删除一个用户
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation("删除一个用户")
    @PreAuthorize("hasAuthority('sys:user:delete')")
    @Log(operation = "删除一个用户")
    public ResponseEntity<Void> delete(@PathVariable("id") Long id) {
        sysUserService.removeById(id);
        return ResponseEntity.ok().build();
    }

    /**
     * 删除多个用户
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("删除多个用户")
    @Log(operation = "删除多个用户")
    public ResponseEntity<Void> delete(@RequestBody List<Long> ids) {
        sysUserService.removeByIds(ids);
        return ResponseEntity.ok().build();
    }

    /**
     * 回显
     */
    @GetMapping("/info/{id}")
    @ApiOperation("用户的回显")
    @PreAuthorize("hasAuthority('sys:user:info')")
    public ResponseEntity<SysUser> findById(@PathVariable("id") Long id) {
        SysUser sysUser = sysUserService.getById(id);
        return ResponseEntity.ok(sysUser);
    }

    /**
     * 修改用户
     *
     * @param sysUser
     * @return
     */
    @PutMapping
    @ApiOperation("修改用户")
    @PreAuthorize("hasAuthority('sys:user:update')")
    @Log(operation = "修改一个用户")
    public ResponseEntity<Void> update(@RequestBody @Validated SysUser sysUser) {
        sysUser.setPassword(new BCryptPasswordEncoder().encode(sysUser.getPassword()));
        sysUserService.updateById(sysUser);
        return ResponseEntity.ok().build();
    }


}
